
SRC_DIR=~/ffmpeg_sources
BUILD_DIR=~/ffmpeg_build
BIN_DIR=~/bin

COMPILE_TOOL_CHAIN="arm-ca7-linux-uclibcgnueabihf"


cd "$SRC_DIR" 

if [ -d "$SRC_DIR/ffmpeg-4.2.1" ]; then
    echo "ffmpeg-4.2.1 dir is exist..."
else
    if [ -f "$SRC_DIR/ffmpeg-4.2.1.tar.bz2" ]; then
        tar xjvf ffmpeg-4.2.1.tar.bz2
    else
        echo "not find ffmpeg-4.2.1.tar.bz2 "
        exit 1
    fi
fi


cd ffmpeg-4.2.1 

# PATH="$BIN_DIR:$PATH" PKG_CONFIG_PATH="$BUILD_DIR/lib/pkgconfig" CFLAGS="-O3 -fPIC" 
PATH="$BIN_DIR:$PATH" PKG_CONFIG_PATH="$BUILD_DIR/lib/pkgconfig" CFLAGS="" 

# 配置参数拼接
CONFIGURE_CMD="./configure"
CONFIGURE_CMD+=" --prefix=\"$BUILD_DIR\""
CONFIGURE_CMD+=" --extra-cflags=\"-I$BUILD_DIR/include  \""
CONFIGURE_CMD+=" --extra-ldflags=\"-L$BUILD_DIR/lib \""
CONFIGURE_CMD+=" --extra-libs=\"-lpthread -lm\""
CONFIGURE_CMD+=" --bindir=\"$BIN_DIR\""
CONFIGURE_CMD+=" --enable-cross-compile"
CONFIGURE_CMD+=" --cross-prefix=$COMPILE_TOOL_CHAIN-"
CONFIGURE_CMD+=" --arch=arm"
CONFIGURE_CMD+=" --target-os=linux"
CONFIGURE_CMD+=" --enable-optimizations"
CONFIGURE_CMD+=" --enable-pic"
CONFIGURE_CMD+=" --enable-small"
CONFIGURE_CMD+=" --enable-neon"
CONFIGURE_CMD+=" --enable-vfp"
CONFIGURE_CMD+=" --enable-thumb"
CONFIGURE_CMD+=" --enable-asm"
CONFIGURE_CMD+=" --enable-inline-asm"
# CONFIGURE_CMD+=" --enable-hardcoded-tables"
CONFIGURE_CMD+=" --disable-static"
CONFIGURE_CMD+=" --enable-shared"
CONFIGURE_CMD+=" --disable-doc"
CONFIGURE_CMD+=" --cpu=cortex-a53"



# 组件精准控制
CONFIGURE_CMD+=" --enable-avfilter"
CONFIGURE_CMD+=" --enable-avformat"
CONFIGURE_CMD+=" --enable-avcodec"
CONFIGURE_CMD+=" --enable-avutil"
CONFIGURE_CMD+=" --enable-swresample"
CONFIGURE_CMD+=" --disable-everything"
CONFIGURE_CMD+=" --enable-filter=aresample,aformat,equalizer"
CONFIGURE_CMD+=" --enable-protocol=file" # Cannot open input file , err=Protocol not found
# 常见PCM和G711解码器
# 常见PCM和G711编码器
CONFIGURE_CMD+=" --enable-decoder=wav,pcm_s16le,pcm_alaw,pcm_mulaw,pcm_u8,pcm_f32le,pcm_f64le,pcm_s24le,pcm_s32le,pcm_zork,pcm_s16be,pcm_s24be,pcm_s32be,pcm_u16le,pcm_u16be,pcm_u24le,pcm_u24be,pcm_u32le,pcm_u32be,pcm_s8,pcm_u8,adpcm_g726,g726,pcm_g711_alaw,pcm_g711_mulaw,g711,pcm_g711"  
CONFIGURE_CMD+=" --enable-encoder=wav,pcm_s16le,pcm_alaw,pcm_mulaw,pcm_u8,pcm_f32le,pcm_f64le,pcm_s24le,pcm_s32le,pcm_zork,pcm_s16be,pcm_s24be,pcm_s32be,pcm_u16le,pcm_u16be,pcm_u24le,pcm_u24be,pcm_u32le,pcm_u32be,pcm_s8,pcm_u8,adpcm_g726,g726,pcm_g711_alaw,pcm_g711_mulaw,g711,pcm_g711"  
CONFIGURE_CMD+=" --enable-muxer=wav,adts,pcm_alaw,pcm_mulaw"
CONFIGURE_CMD+=" --enable-demuxer=wav,pcm_alaw,pcm_mulaw"



# --enable-avfilter \
# --enable-avformat \
# --enable-avcodec \
# --enable-avutil \
# --enable-swresample \
# --disable-everything \
# --enable-filter=aresample,aformat,... \
# --enable-decoder=pcm_s16le,mp3,aac,... \
# --enable-demuxer=wav,mp3,aac,...


# 基础配置模板
# ./configure \
# --arch=arm \
# --cpu=cortex-a53 \              # 必须与目标芯片匹配
#  --enable-cross-compile \
#  --cross-prefix=$CROSS_PREFIX \
#  --sysroot=$SYSROOT \
#  --target-os=linux \
#  --prefix=./output \
#  --enable-pic \
#  --enable-small \
#  --enable-neon \
#  --enable-vfp \
#  --enable-thumb \
#  --enable-asm \
#  --enable-inline-asm \
#  --enable-hardcoded-tables \
#  --disable-runtime-cpudetect \     # 固定设备启用
#  --disable-static \
#  --enable-shared \
#  --disable-doc





# 显示最终执行的 configure 命令
echo "========== start configure eval"
echo $CONFIGURE_CMD
# 执行 configure
eval $CONFIGURE_CMD
echo "========== end configure eval"
PATH="$BIN_DIR:$PATH" make && \
make install && \
hash -r
source ~/.profile
cp $SRC_DIR/script_ffmpeg/ffmpeg.examples.Makefile $BUILD_DIR/share/ffmpeg/examples/Makefile

cp -rf $BUILD_DIR/lib ~/ffmpeg_mount/

# ./configure \
#     --prefix="$BUILD_DIR" \
#     --pkg-config-flags="--static" \
#     --extra-cflags="-I$BUILD_DIR/include -O0 -g" \
#     --extra-ldflags="-L$BUILD_DIR/lib -g" \
#     --extra-libs="-lpthread -lm" \
#     --bindir="$BIN_DIR" \
#     --enable-gpl \
#     --disable-asm \
#     --enable-version3 \
#     --cross-prefix=$COMPILE_TOOL_CHAIN- \
#     -cc=$COMPILE_TOOL_CHAIN-gcc \
#     \
#     --arch=arm \
#     --enable-parser=mpegaudio \
#     --enable-protocol=file \
#     \
#     --enable-avcodec \
#     --enable-avfilter \
#     --enable-muxer=wav \
#     --enable-demuxer=wav \
#     --enable-avformat \
#     --enable-avutil \
#     \
#     --target-os=linux \
#     --enable-cross-compile \
#     --enable-small \
#     --enable-shared \
#     --enable-nonfree \
#     --enable-protocols \
#     \
#     --enable-debug \
#     --disable-stripping \
#     --disable-yasm \
#     --disable-x86asm \
#     --enable-pic \
#     --enable-neon \
#     --enable-thumb \
#     --enable-hardcoded-tables \



# 基础配置模板
# ./configure \
# --arch=arm \
# --cpu=cortex-a53 \              # 必须与目标芯片匹配
#  --enable-cross-compile \
#  --cross-prefix=$CROSS_PREFIX \
#  --sysroot=$SYSROOT \
#  --target-os=linux \
#  --prefix=./output \
#  --enable-pic \
#  --enable-small \
#  --enable-neon \
#  --enable-vfp \
#  --enable-thumb \
#  --enable-asm \
#  --enable-inline-asm \
#  --enable-hardcoded-tables \
#  --disable-runtime-cpudetect \     # 固定设备启用
#  --disable-static \
#  --enable-shared \
#  --disable-doc


# 组件精准控制
# --enable-decoder=h264,h265,mpeg4,aac \
# --enable-encoder=libx264,libfdk_aac \
# --enable-muxer=mp4,flv \
# --enable-demuxer=mov,flv,mpegts \
# --disable-avdevice \
# --disable-postproc \
# --disable-filters \
# --disable-network


# ./configure --prefix=./fmg_zlg --enable-cross-compile --target-os=linux --cross-prefix=/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/ 
# --cc=arm-linux-gnueabihf-gcc --cxx=arm-linux-gnueabihf-g++ --nm=arm-linux-gnueabihf-nm --strip=arm-linux-gnueabihf-strip --ar=arm-linux-gnueabihf-ar 
# --ranlib=arm-linux-gnueabihf-ranlib  --pkg-config=arm-linux-gnueabihf-pkg-config --cpu=cortex-a7 arch=arm32 --disable-yasm --disable-x86asm --disable-static --enable-shared --enable-small --enable-ffmpeg



# --Cc=aarch64-buildroot-1inux-gnu-gcc\
# --enable-cross-compile\
# --disable-x86asm\
# --enable-shared\
# --disable-static\
# --enable-libdrm\
# --enable-rkmpp\
# --enable-rkrga\
# --disable-doc\
# --disable-debug \
# --disable-hardcoded-tables \
# --disable-mipsdsp \
# --disable-mipsfpu \
# --disable-hardcoded-tables \
# --disable-stripping \





# --enable-libass \
# --enable-libfdk-aac \
# --enable-libfreetype \
# --enable-libmp3lame \
# --enable-libopus \
# --enable-libvorbis \
# --enable-libvpx \
# --enable-libx264 \
# --enable-libx265 \
# --enable-pic \



# --enable-cross-compile \
# --cross-prefix=arm-linux-gnueabihf- \
# --arch=arm \
# --target-os=linux \
# --sysroot=/path/to/your/sysroot


# --enable-cross-compile : 开启交叉编译
# --target-os　　 : 目录系统
# --cross-prefix : 编译前缀,通常是指定的编译目录
# --cc : 指编c编译程序
# --cxx : 指定c++编译程序
# --cpu : 指定cpu类型
# -arch : 指定程序运行版本
# --disable-yasm : 不启用汇编指令
# --disable-static : 禁用静态库生成
# --enable-shared : 启用动态库
# --enable-small : 启用最小生成
# --enable-ffmpeg : 生成ffmpeg可执行文件

